package life.mahu.community.controller;

import life.mahu.community.dto.QuestionDTO;
import life.mahu.community.dto.TagDTO;
import life.mahu.community.model.Category;
import life.mahu.community.model.Question;
import life.mahu.community.model.User;
import life.mahu.community.service.CategoryService;
import life.mahu.community.service.QuestionService;
import life.mahu.community.util.SensitiveWordFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class PublishController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SensitiveWordFilter sensitivewordFilter;

    @GetMapping("/publish/{id}")
    public String edit(@PathVariable(name = "id") Long id,
                       Model model) {

        // 查询分类
        List<Category> categoryList = categoryService.getList();

        QuestionDTO question = questionService.getById(id);
        model.addAttribute("title", question.getTitle());
        model.addAttribute("description", question.getDescription());
        model.addAttribute("tag", question.getTag());
        model.addAttribute("id", question.getId());
        List<TagDTO> tagDTOList = categoryService.getTags();
        model.addAttribute("tags", tagDTOList);
        model.addAttribute("categorys", categoryList);
        return "publish";
    }


    @GetMapping("/publish")
    public String publish(Model model) {
        List<TagDTO> tagDTOList = categoryService.getTags();
        List<Category> categoryList = categoryService.getList();
        model.addAttribute("categorys", categoryList);
        model.addAttribute("tags", tagDTOList);
        return "publish";
    }

    @PostMapping("/publish")
    public String doPublish(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "description", required = false) String description,
            //@RequestParam(value = "tags", required = false) int[] tags,
            @RequestParam(value = "tag", required = false) String tag,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "cid", required = false) Integer cid,
            HttpServletRequest request,
            Model model) {
        model.addAttribute("title", title);
        model.addAttribute("description", description);
        model.addAttribute("tag", tag);
        List<TagDTO> tagDTOList = categoryService.getTags();
        model.addAttribute("tags", tagDTOList);
        List<Category> categoryList = categoryService.getList();
        model.addAttribute("categorys", categoryList);

        if (StringUtils.isBlank(title)) {
            model.addAttribute("error", "标题不能为空");
            return "publish";
        }
        if (StringUtils.isBlank(title)) {
            model.addAttribute("error", "标题不能为空");
            return "publish";
        }
        if ("".equals(cid)||cid==null||cid == 0) {
            cid = categoryList.get(0).getId();
        }

//        if (StringUtils.isNotBlank(invalid)) {
//            model.addAttribute("error", "输入非法标签:" + invalid);
//            return "publish";
//        }

        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            model.addAttribute("error", "用户未登录");
            return "publish";
        }

        //如果用户被禁言了
        if(user.getType()==-1){
            model.addAttribute("error", "你已被禁言，如有疑问请与管理员联系");
            return "publish";
        }

        //敏感词过滤
        if(sensitivewordFilter.isContaintSensitiveWord(title,1) ){
            model.addAttribute("error", "非法标题！根据相关法律法规政策，提问未能正常发布！互联网并非法外之地！发言三思后行！");
            return "publish";
        }
        if(sensitivewordFilter.isContaintSensitiveWord(description,1)){
            model.addAttribute("error", "非法描述！根据相关法律法规政策，提问未能正常发布！互联网并非法外之地！发言三思后行！");
            return "publish";
        }
        if(sensitivewordFilter.isContaintSensitiveWord(tag,1)){
            model.addAttribute("error", "非法标签！根据相关法律法规政策，提问未能正常发布！互联网并非法外之地！发言三思后行！");
            return "publish";
        }


        Question question = new Question();
        question.setTitle(title);
        question.setDescription(description);
        question.setTag(tag);
        question.setCreator(user.getId());
        question.setId(id);
        question.setCid(cid);
        questionService.createOrUpdate(question,tag);
        return "redirect:/";
    }
}
